#include "svgtriangleitem.h"

SvgTriangleItem::SvgTriangleItem(QPointF start, QPointF end, int Width, QColor color, QBrush brush_input)
    :CShape(start,end, Width, color, brush_input)
{

}

SvgTriangleItem::SvgTriangleItem(qreal start_x,qreal start_y, qreal end_x, qreal end_y,int Width, QColor color, QBrush brush_input):
    CShape(start_x, start_y, end_x, end_y,Width, color, brush_input)
{
}


void SvgTriangleItem::paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget ) {
    Q_UNUSED(widget);
    painter->setPen(this->pen());              //设置画笔样式
    painter->setBrush(this->brush());
    QPainterPath path;
    path.moveTo(points[S1]);                   //绘制三角形路径
    path.lineTo(points[S2]);
    path.lineTo(points[E1]);
    path.closeSubpath();
    painter->drawPath(path);

    QPen pen("black");
    pen.setWidth(1);
    painter->setPen(pen);
    painter->setBrush(Qt::NoBrush);
    if(option->state & QStyle::State_Selected) {
        GenerateControlPoints();
        for (int i= 0;i < 3; i++ ) {             //将选中控制点的矩形框填充为红色
            if(i!=WitchPointsisSelected) painter->drawRect(PointsforDraw[i]);
            else {
                painter->setBrush(Qt::red);
                painter->drawRect(PointsforDraw[i]);
                painter->setBrush(Qt::NoBrush);
            }
        }
    }
}

QPainterPath SvgTriangleItem::shape() const{
    QPainterPath path;                    //绘制三角形路径
    path.moveTo(points[S1]);
    path.lineTo(points[S2]);
    path.lineTo(points[E1]);
    path.closeSubpath();
     // 创建一个 QPainterPathStroker 对象，并设置其宽度为当前画笔的宽度
    QPainterPathStroker stroker;
    stroker.setWidth(pen().widthF()+10);
     // 使用 QPainterPathStroker对象对路径进行描边，并得到描边后的路径
    QPainterPath strokerPath = stroker.createStroke(path);
     // 将控制点加入路径中以保证其能被选中
    for (int i = 0; i < 3; i++) {
        strokerPath.addRect(PointsforDraw[i]);
    }
     // 如果未填充视为线条，如果填充视为图形，即将内部也加入shape
    if(brush() != Qt::NoBrush) strokerPath.addRect(path.boundingRect());
    return strokerPath;
}

QRectF SvgTriangleItem::boundingRect() const {
    return shape().boundingRect();
}

void SvgTriangleItem::mousePressEvent(QGraphicsSceneMouseEvent* event) {
    WitchPointsisSelected = -1;
    isRotating = false;
    if (event->buttons() & Qt::LeftButton && event->modifiers() & Qt::ShiftModifier) {
        isRotating = true;
        BeginScenePos = event->scenePos();
        BeginTransform = transform();
    }
    else {
        setCursor(Qt::CursorShape::SizeAllCursor);
        for (int i = 0; i < 3 ; i++) {
            if (PointsforDraw[i].contains(event->pos())) {
                WitchPointsisSelected = i;
                setSelected(true);
                break;
            }
        }
        if (WitchPointsisSelected != -1) {
            // 将被选中点的位置更新到鼠标位置实现吸附效果
            points[WitchPointsisSelected] = event->pos();
            points[WitchPointsisSelected] = event->pos();
            GenerateControlPoints();
            prepareGeometryChange();
            update();
        }
        else QGraphicsItem::mousePressEvent(event);
    }
}
void SvgTriangleItem::mouseMoveEvent(QGraphicsSceneMouseEvent* event) {
    if (WitchPointsisSelected != -1)
    {
        setSelected(true);
        points[WitchPointsisSelected] = event->pos();
        prepareGeometryChange();
        update();
    }
    else     if (isRotating&&(event->buttons() & Qt::LeftButton && event->modifiers() & Qt::ShiftModifier)) {
        setSelected(true);
        SupportRotate(event->scenePos(),mapToScene(boundingRect().center()));
    }
    else QGraphicsItem::mouseMoveEvent(event);
}
void SvgTriangleItem::mouseReleaseEvent(QGraphicsSceneMouseEvent* event) {
    setCursor(Qt::CursorShape::ArrowCursor);
    if(WitchPointsisSelected != -1) {
        WitchPointsisSelected = -1;
        update();
    }
    QGraphicsItem::mouseReleaseEvent(event);
}
